# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 30 2011)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc
from Data.Player import ClickPlayer, MovePlayer

###########################################################################
## Class DrawPanel
###########################################################################

class DrawPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__( self, parent)
        self.__mController = None
        
        self.SetFocus()
        self.AcceptsFocus() 
        self.SetFocusFromKbd()
        self.__mLastPointClick = (-100, -100)
        self.__mLastMove = (50, 50)
        self.__mBitePoint = (-100, -100)
        
        wx.EVT_PAINT(self, self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
    
    def SetController(self, inController):
        self.__mController = inController
        
    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        self.Draw(dc)
    
    def OnLeftClick(self, event):
        '''
        event left click
        '''
        if self.__mController is None: return
        self.__mController.SendClickPlayer(ClickPlayer(self.__mController.GetPlayer(), (event.GetX(), event.GetY())))
        event.Skip()
    
    def OnKeyDown(self, event):
        theKeyCode = event.GetKeyCode()
        theLastX, theLastY = self.__mLastMove
        theMoveX, theMoveY = self.__mBitePoint
        theValueMove = 2
        if theKeyCode == 83:
            #Faire bouger le stickman vers le bas
            theLastY += theValueMove
            theMoveY += theValueMove
        elif theKeyCode == 90:
            #Faire bouger le stickman vers le haut
            theLastY -= theValueMove
            theMoveY -= theValueMove
        elif theKeyCode == 68:
            #Faire bouger le stickman vers le Droite
            theLastX += theValueMove
            theMoveX += theValueMove
        elif theKeyCode == 81:
            #Faire bouger le stickman vers le gauche
            theLastX -= theValueMove
            theMoveX -= theValueMove
        self.__mController.SendMovePlayer(MovePlayer(self.__mController.GetPlayer(), (theLastX, theLastY), (theMoveX, theMoveY)))
            
    def AddClickPlayer(self, inClickPlayer):
        '''
        action when a player click on the panel
        '''
        self.__mLastPointClick = inClickPlayer.GetPosition()
        self.Refresh()
    
    def AddMovePlayer(self, inMovePlayer):
        self.__mLastMove = inMovePlayer.GetPosition()
        self.Refresh()
        
    def Draw(self, dc):
        '''
        function to draw the picture
        '''
        dc.BeginDrawing()
        dc.SetBackground( wx.Brush("White") )
        dc.SetPen(wx.Pen('VIOLET', 1))
        dc.Clear()
        if self.__mController is None: return
        theX, theY = self.__mLastPointClick
        dc.DrawCircle(theX, theY, 5)
        self.DrawStickMan(dc,  self.__mLastMove)
        dc.EndDrawing()
        
        
    def DrawStickMan(self, dc, inPosition):
        theX, theY = inPosition
        theHeadRadius = 10
        theHeadPosionX = theX + 20
        theHeadPosionY = theY + 20
        
        theNeckLength = theHeadRadius * 3.5
        theNeckUpX = theHeadPosionX
        theNeckUpY = theHeadPosionY + theHeadRadius
        theNeckDownX = theNeckUpX
        theNeckDownY = theNeckUpY  + theNeckLength
        
        
        theArmLength = theHeadRadius * 2
        theArmRightUpX = theNeckUpX
        theArmRightUpY = theNeckUpY
        theArmRightDownX = theHeadPosionX - theArmLength
        theArmRightDownY = theNeckUpY  + theNeckLength / 2
        
        theArmLeftUpX = theNeckUpX
        theArmLefttUpY = theNeckUpY
        theArmLeftDownX = theHeadPosionX + theArmLength
        theArmLeftDownY = theNeckUpY  + theNeckLength / 2
        
        
        theLegLength = theHeadRadius * 2.5
        theDecalLeg = theArmLength * 1.5
        theLegRightUpX = theNeckDownX
        theLegRightUpY = theNeckDownY
        theLegRightDownX = theHeadPosionX - theLegLength
        theLegRightDownY = theNeckDownY  + theDecalLeg
        
        theLegLeftUpX = theNeckDownX
        theLegLefttUpY = theNeckDownY
        theLegLeftDownX = theHeadPosionX + theLegLength
        theLegLeftDownY = theNeckDownY   + theDecalLeg
        
        self.__mBitePoint = (theNeckDownX, theNeckDownY)
        
        dc.DrawCircle(theHeadPosionX, theHeadPosionY, theHeadRadius)
        dc.DrawLine(theNeckUpX, theNeckUpY, theNeckDownX, theNeckDownY)
        dc.DrawLine(theArmRightUpX, theArmRightUpY, theArmRightDownX, theArmRightDownY)
        dc.DrawLine(theArmLeftUpX, theArmLefttUpY, theArmLeftDownX, theArmLeftDownY)
        dc.DrawLine(theLegRightUpX, theLegRightUpY, theLegRightDownX, theLegRightDownY)
        dc.DrawLine(theLegLeftUpX, theLegLefttUpY, theLegLeftDownX, theLegLeftDownY)
    
    def __del__( self ):
        pass


